<!--<template>-->
<!--  <div >-->
<!--    <h1 v-if="level === 1">-->
<!--      <slot></slot>-->
<!--    </h1>-->
<!--    <h2 v-else-if="level === 2">-->
<!--      <slot></slot>-->
<!--    </h2>-->
<!--    <h3 v-else-if="level === 3">-->
<!--      <slot></slot>-->
<!--    </h3>-->
<!--    <h4 v-else-if="level === 4">-->
<!--      <slot></slot>-->
<!--    </h4>-->
<!--    <h5 v-else-if="level === 5">-->
<!--      <slot></slot>-->
<!--    </h5>-->
<!--    <h6 v-else-if="level === 6">-->
<!--      <slot></slot>-->
<!--    </h6>-->
<!--  </div>-->
<!--</template>-->

<script>
import html2canvas from 'html2canvas'
export default {
  name: 'anchored-heading',
  props: {
    level: {
      type: Number,
      required: true
    }
  },
  data: function () {
    return {
      posterUrl: ''
    }
  },
  methods: {
    clickH: function () {
      // console.log(this.$parent.$refs.imageTofile)
      return html2canvas(this.$parent.$refs.imageTofile, {
        backgroundColor: null, // 背景颜色
        dpi: 192, // 将分辨率提高到特定的dpi,默认值为96
        scale: 2, // 用于渲染的比例尺。默认为浏览器设备像素比率。默认值是1，手机端设置成2
        useCORS: true // 是否尝试使用CORS从服务器加载图像
      }).then((canvas) => {
        console.log('成功')
        this.posterUrl = canvas.toDataURL('image/png')
        this.downloadFile('截屏.png', this.posterUrl)
      })
    },
    // 下载
    downloadFile (fileName, content) {
      let aLink = document.createElement('a')
      let blob = this.base64ToBlob(content) // new Blob([content]);

      let evt = document.createEvent('HTMLEvents')
      console.log('点击下载', evt)
      evt.initEvent('click', true, true)// initEvent 不加后两个参数在FF下会报错  事件类型，是否冒泡，是否阻止浏览器的默认行为
      aLink.download = fileName
      aLink.href = URL.createObjectURL(blob)

      // aLink.dispatchEvent(evt);
      aLink.click()
    },
    // base64转blob
    base64ToBlob (code) {
      let parts = code.split(';base64,')
      let contentType = parts[0].split(':')[1]
      let raw = window.atob(parts[1])
      let rawLength = raw.length

      let uInt8Array = new Uint8Array(rawLength)

      for (let i = 0; i < rawLength; ++i) {
        uInt8Array[i] = raw.charCodeAt(i)
      }
      return new Blob([uInt8Array], {type: contentType})
    }
  },
  components: {
  },
  render: function (createElement) {
    return createElement(
      'h' + this.level,
      {
        style: {
          color: 'red'
        },
        on: {
          click: this.clickH
        }
      },
      [this.$slots.default, createElement('a', {
        style: {
          color: 'yellow'
        }
      }, '点击截图')]
    )
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.chat-wrap{
  background-color: #283046;
  height: 100%;
  border-radius: 6px;
}
</style>
